package com.cxhome.cxinfox.order;

import org.apache.log4j.Logger;

import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.base.BaseService;
import com.traffic.managent.base.Paginator;
import com.traffic.managent.model.system.Department;
import com.traffic.managent.util.StringUtils;

/**
 * 客户业务处理类。
 * 
 */
public class OrderService extends BaseService {

	private final static Logger log = Logger.getLogger(OrderService.class);
	public static final OrderService service = new OrderService();

	public Page<Order> queryListForCus(Order model, Paginator paginate, StringBuilder orderSql, String customerId,
			String departmentId, String start, String end) {

		// 用户没有绑定账户时
		String idsStr = "";
		if (StringUtils.isEmpty(customerId)) { // 管理员用户登录时,没有账户信息,根据组织机构来查询数据
			String tempIdsStr = Department.dao.queryChildIds(departmentId);
			String tempIdsStr1 = tempIdsStr.replaceAll(",", "','");
			if (tempIdsStr1.contains(",")) {
				idsStr = "('" + tempIdsStr1 + "')";
			} else {// 组织机构只有一个时查询语句会报错
				idsStr = "('" + tempIdsStr1 + "','" + tempIdsStr1 + "')";
			}
			// 加入组织结构参数
			departmentId = idsStr;
			customerId = "";
		} else {
			// 有绑定账户时就只能够查询该账户的充值记录
			departmentId = "";
		}

		StringBuilder sql = new StringBuilder(
				" FROM cx_order o LEFT JOIN cx_customer_info c  ON o.customerId = c.customerId WHERE 1=1 and o.nowRoute = '0' ");

		if (StringUtils.isEmpty(customerId)) {
			sql.append(" AND o.department in " + idsStr);
		} else {
			sql.append(" AND c.customerId = '" + customerId + "'");
		}
		// 订单状态
		if (StringUtils.isNotEmpty(model.getStr("status"))) {
			sql.append(" AND  o.status =  '" + model.getStr("status") + "'");
		}
		if (StringUtils.isNotEmpty(model.getStr("orderNo"))) {
			sql.append("AND  o.orderNo like '%" + model.getStr("orderNo") + "%'");
		}
		if (StringUtils.isNotEmpty(model.getStr("phoneNo"))) {
			sql.append("AND  o.phoneNo like '%" + model.getStr("phoneNo") + "%'");
		}
		if (StringUtils.isNotEmpty(model.getStr("operator"))) {
			sql.append(" AND  o.operator =  '" + model.getStr("operator") + "'");
		}
		if (start != null && !"".equals(start)) {
			sql.append(" and o.orderTime >= '" + start + "'");
		}
		if (end != null && !"".equals(end)) {
			sql.append(" and o.orderTime <= '" + end + "'");
		}
		sql.append(orderSql);
		return Order.dao.paginate(paginate, "select o.*,c.customerId,c.customerName,o.cstmOrderNo  ", sql.toString());

	}

	public Page<Order> queryListForErr(Order model, Paginator paginate, StringBuilder orderSql, String customerId,
			String departmentId, String start, String end) {

		StringBuilder idsStr = new StringBuilder("");
		if (StringUtils.isEmpty(customerId)) {
			// ***从组织机构数据表中查询出该组织结构所有的实际子组织机构id(含本身)***
			String tempIdsStr = Department.dao.queryChildIds(departmentId);
			String tempIdsStr1 = tempIdsStr.replaceAll(",", "','");
			if (tempIdsStr1.contains(",")) {
				idsStr.append("('" + tempIdsStr1 + "')");
			} else {// 组织机构只有一个时查询语句会报错
				idsStr.append("('" + tempIdsStr1 + "','" + tempIdsStr1 + "')");
			}
			// 加入组织结构参数
			departmentId = idsStr.toString();
			customerId = "";
		} else {
			// 有绑定账户时就只能够查询该账户的充值记录
			departmentId = "";
		}

		StringBuilder sql = new StringBuilder(
				"  from cx_order o left join cx_customer_info c on o.customerId = c.customerId where 1=1 ");

		if (StringUtils.isEmpty(customerId)) {
			sql.append(" AND o.department in " + idsStr);
		} else {
			sql.append(" AND c.customerId = '" + customerId + "'");
		}

		if (StringUtils.isNotEmpty(model.getStr("payMode"))) {
			sql.append(" and  o.payMode = '" + model.getStr("payMode") + "'");
		}

		if (StringUtils.isNotEmpty(model.getStr("customerId"))) {
			sql.append(" and  o.customerId = '" + model.getStr("customerId") + "'");
		}

		if (StringUtils.isNotEmpty(model.getStr("ids"))) {
			sql.append(" and  o.ids like '" + model.getStr("ids") + "'");
		}

		if (StringUtils.isNotEmpty(model.getStr("phoneNo"))) {
			sql.append(" and  o.phoneNo like '" + model.getStr("phoneNo") + "'");
		}

		if (StringUtils.isNotEmpty(model.getStr("operator"))) {
			sql.append(" and  o.operator = '" + model.getStr("operator") + "'");
		}

		if (model.getStr("status") != null && !"".equals(model.getStr("status"))) {
			if ("1".equals(model.getStr("status"))) {
				sql.append(" and  o.status = '1'  ");
			} else if ("0".equals(model.getStr("status"))) {
				sql.append(
						" and  ((o.status = '0' and o.payMode = '0') or (o.status = '0' and o.payMode = '1' and o.isPay = '0')) ");
			}
		} else {
			sql.append(
					" and  (o.status = '1' or (o.status = '0' and o.payMode = '0') or (o.status = '0' and o.payMode = '1' and o.isPay = '0'))");
		}

		if (start != null && !"".equals(start)) {
			sql.append(" and  o.orderTime >= '" + start + "'");
		}
		if (end != null && !"".equals(end)) {
			sql.append(" and  o.orderTime <= '" + end + "'");
		}

		sql.append(" and DATE_ADD(o.orderTime,INTERVAL 15 MINUTE)<now()");
		sql.append(orderSql);

		return Order.dao.paginate(paginate, "select o.*,c.customerId,c.customerName ", sql.toString());

	}

	public Page<Order> queryListForVas(Order model, Paginator paginate, StringBuilder orderSql, String departmentId,
			String start, String end) {

		// ***从组织机构数据表中查询出该组织结构所有的实际子组织机构id(含本身)***
		String tempIdsStr = Department.dao.queryChildIds(departmentId);
		String tempIdsStr1 = tempIdsStr.replaceAll(",", "','");
		String idsStr = "";
		if (tempIdsStr1.contains(",")) {
			idsStr = "('" + tempIdsStr1 + "')";
		} else {// 组织机构只有一个时查询语句会报错
			idsStr = "('" + tempIdsStr1 + "','" + tempIdsStr1 + "')";
		}
		StringBuilder sql = new StringBuilder(
				" FROM cx_order o LEFT JOIN cx_customer_info c ON o.customerId = c.customerId  WHERE 1=1 ");

		sql.append(" AND o.department in " + idsStr);

		if (StringUtils.isNotEmpty(model.getStr("status"))) {
			sql.append("and  o.status = '" + model.getStr("status") + "'");
		}
		if (StringUtils.isNotEmpty(model.getStr("customerId"))) {
			sql.append("and  c.customerId = '" + model.getStr("customerId") + "'");
		}
		if (StringUtils.isNotEmpty(model.getStr("cstmOrderNo"))) {
			sql.append("and o.cstmOrderNo like '%" + model.getStr("cstmOrderNo") + "%'");
		}
		if (StringUtils.isNotEmpty(model.getStr("ids"))) {
			sql.append("and o.ids like '%" + model.getStr("ids") + "%'");
		}
		if (StringUtils.isNotEmpty(model.getStr("phoneNo"))) {
			sql.append("and o.phoneNo like '%" + model.getStr("phoneNo") + "%'");
		}
		if (StringUtils.isNotEmpty(model.getStr("operator"))) {
			sql.append("and o.operator = '" + model.getStr("operator") + "'");
		}
		if (StringUtils.isNotEmpty(model.getStr("providerId"))) {
			sql.append("and o.providerId = '" + model.getStr("providerId") + "'");
		}
		if (start != null && !"".equals(start)) {
			sql.append(" and  o.orderTime >= '" + start + "'");
		}
		if (end != null && !"".equals(end)) {
			sql.append(" and  o.orderTime <= '" + end + "'");
		}
		sql.append(orderSql);
		return Order.dao.paginate(paginate, "select o.*,c.customerId,c.customerName,o.cstmOrderNo ", sql.toString());

	}

}
